/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.contacts;

import java.io.Serializable;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.async.LoadNetworkImageTask;
import com.phonebooksharing.android.util.GalleryUtilities;
import com.phonebooksharing.android.util.GeneralUtils;
import com.phonebooksharing.android.util.Global;
import com.phonebooksharing.android.widgets.drawable.FastBitmapDrawable;
import com.phonebooksharing.android.widgets.pagination.PaginatorListView;
import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.dto.gallery.WishlistItem;

public class ContactWishlistGalleryListView extends PaginatorListView {

    private ContactItem focusFriend;
    private LayoutInflater inflater;
    public ContactWishlistGalleryListView(Context dv, int qty, TextView counter, ContactItem focusFriend) {
        super(dv, qty, counter);
        this.focusFriend = focusFriend;
        inflater = LayoutInflater.from(getContext());
    }

    @Override
    public String getLoadDataURL() {
        return String.format(Global.WISHLIST_SERVLET_URL, "gwl" /*ac*/, "" /*q*/, focusFriend.andID /*c*/, currentPage++ /*p*/, Global.PAGINATOR_PAGE_SIZE /*ps*/);
    }

    @Override
    public Serializable getObject2Send() {
        return Global.authItem;
    }

    @Override
    public View getConvertView(View convertView, Object dto, int position, ViewGroup parent) {
        ViewHolder holder;
        
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_3lines_check, null);

            holder = new ViewHolder();
            holder.icon2 = (ImageView) convertView.findViewById(android.R.id.icon);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.time = (TextView) convertView.findViewById(R.id.date);
            holder.text1 = (TextView) convertView.findViewById(R.id.title);
            holder.text2 = (TextView) convertView.findViewById(R.id.text);
            
            holder.type = (ImageView) convertView.findViewById(R.id.type);
            holder.check = (ImageView) convertView.findViewById(R.id.check);
            
            holder.check.setVisibility(View.GONE);
            holder.type.setVisibility(View.GONE);
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final WishlistItem cache = (WishlistItem)dto;
        
        holder.id = cache.wishlistID;
        holder.name.setText(cache.prodName);
        holder.text1.setText("By: " + cache.prodAuthor);
        
        final FastBitmapDrawable prodImg = GalleryUtilities.getFromWishlistCache(cache.prodID);
        if(prodImg != null) {
            holder.icon2.setImageBitmap(prodImg.getBitmap());
        } else {
            holder.icon2.setImageBitmap(GalleryUtilities.loadingBitmap.getBitmap());
            //get video thumb
            final String url = String.format(cache.prodImgUrl);
            LoadNetworkImageTask task = new LoadNetworkImageTask(this, url, true);
            task.externalID = cache.prodID;
            task.execute(cache.wishlistID, 60, 60, position, true /*isThumbnail*/, android.R.id.icon);
        }

        holder.time.setText("(" + DateUtils.formatElapsedTime((GeneralUtils.calculateGmtTimeInMilis(System.currentTimeMillis()) - 
                                                               GeneralUtils.calculateGmtTimeInMilis(cache.when) ) / 1000) + ")");
        
        return convertView;
    }
    
    static class ViewHolder {
        ImageView icon2, type, check;
        TextView name, time, text1, text2;
        View container, border;
        long id;
    }
}
